// page/order/order.js
var sliderWidth = 96; // 需要设置slider的宽度，用于计算中间位置
Page({
  data: {
      isHide: 0,
      tabs: ["全部","待付款", "待发货", "待收货", "待评价"],  
      activeIndex: 0,
      sliderOffset: 0,
      allOrder: [],               //全部订单
      waitPayment: [],       //待付款
      waitDistribution: [],  //待发货
      waitReceiving: [],     //待收货
      waitEvaluation: [],    //待评价
      width: ""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
      var that = this;
      var app = getApp()
      var uid = wx.getStorageSync('uid');
    
         
            wx.request({
              url: app.globalData.host + '&g=App&m=Order&a=order', //仅为示例，并非真实的接口地址
              data: { uid: uid},
              header: {
                'content-type': 'application/json'
              },
              success: function (res) {
                var arr=res.data.d
                if (arr){
                  that.fenlei(arr)
                }
             
              }
            })
            that.setData({ isHide: 1 })     

  },

          
    tabClick: function (e) {
      this.setData({
          sliderOffset: e.currentTarget.offsetLeft,
          activeIndex: e.currentTarget.id
      });
    },
    onShow: function () {
      var that = this
      wx.getStorage({
        key: 'orderTab',
        success: function (res) {
          if (!res.data) {
            that.setData({
              activeIndex: 0
            })
          } else {
            that.setData({
              activeIndex: res.data,
              sliderOffset: that.data.width / that.data.tabs.length * res.data
            })
          }
        },
      })
    }, 
    qxdd:function(e){
      var that = this;
      var i = e.currentTarget.dataset.id
      var order_sn = e.currentTarget.dataset.ordersn
      var arr = that.data.allOrder
      var app = getApp()
  
      for (var i = 0; i <= arr.length - 1; i++) {
        if (arr[i]['orderNo'] == order_sn) {
          arr.splice(i, 1)
          wx.request({
            url: app.globalData.host + '&g=App&m=Order&a=delorder', //仅为示例，并非真实的接口地址
            data: { order_sn: order_sn },
            header: {
              'content-type': 'application/json'
            },
            success: function (res) {
              wx.showToast({
                title: '取消订单成功',
                icon: 'success',
                duration: 2000
              })
              
              that.fenlei(arr)

            }
          })
          break
        }
      }
      
 
      
    },
    fenlei:function(arr){
      var that = this;
      // console.log(arr)
      var waitPayment=[]
      var waitDistribution= []
      var waitReceiving= []
      var waitEvaluation= []

      console.log(arr)
      for (var i = 0; i <= arr.length - 1; i++) {
        if (arr[i]['status'] == 10) {
          arr[i]['status1'] = '待付款'
          waitPayment.push(arr[i])
        }
        else if (arr[i]['status'] == 20) {
          arr[i]['status1'] = '等待卖家发货'
          waitDistribution.push(arr[i])
        }
        else if (arr[i]['status'] == 30) {
          arr[i]['status1'] = '卖家已发货'
          waitReceiving.push(arr[i])
        }
        else if (arr[i]['status'] == 40) {
          arr[i]['status1'] = '待评价'
          waitEvaluation.push(arr[i])
          
        }else{
          arr[i]['status1'] = '已完成'
        }
      }
      wx.getSystemInfo({
        success: function (res1) {
          that.setData({
            allOrder: arr,
            waitPayment: waitPayment,
            waitDistribution: waitDistribution,
            waitReceiving: waitReceiving,
            waitEvaluation: waitEvaluation,
           
            sliderLeft: (res1.windowWidth / that.data.tabs.length - sliderWidth) / 2,
            sliderOffset: res1.windowWidth / that.data.tabs.length * that.data.activeIndex,
            width: res1.windowWidth,
          })
          wx.getStorage({
            key: 'orderTab',
            success: function (res) {
              if (!res.data) {
                that.setData({
                  activeIndex: 0,
                 
                })
              } else {
                that.setData({
                  activeIndex: res.data,
                  sliderOffset: that.data.width / that.data.tabs.length * res.data,
                 
                })
              }
            },
          })
         }
      }); 
     
     
    },
    qrsh:function(e){
      var that = this;
      var order_sn = e.currentTarget.dataset.ordersn
      var arr = that.data.allOrder
      var app = getApp()
      wx.showModal({
        title: '提示',
        content: '确认收货吗？',
        success: function (res) {
          if (res.confirm) {
            for (var i = 0; i <= arr.length - 1; i++) {
              if (arr[i]['orderNo'] == order_sn) {
                arr[i]['status'] = 40
                console.log(arr[i])
                wx.request({
                  url: app.globalData.host + '&g=App&m=Order&a=daoorder', //仅为示例，并非真实的接口地址
                  data: { order_sn: order_sn },
                  header: {
                    'content-type': 'application/json'
                  },
                  success: function (res) {
                    wx.showToast({
                      title: '确认收货成功',
                      icon: 'success',
                      duration: 2000
                    })
                    wx.navigateTo({
                      url: 'comment'
                    })
                  }
                })
                break
              }
            }
            that.fenlei(arr)
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }
      })
     
    },
    ddfk:function(e){
      var that = this;
      var order_sn = e.currentTarget.dataset.ordersn
      var price = e.currentTarget.dataset.orderprice
      var arr = that.data.allOrder
      var app = getApp()
      wx.setStorageSync('order_sn', order_sn) // 存储
      wx.setStorageSync('sum', price) // 存储
      wx.navigateTo({
        url: '/page/cart/payment'
      })
    },orderinfo:function(e){
      var order_sn = e.currentTarget.dataset.ordersn
      // console.log(order_sn)
      wx.navigateTo({
        url: '/page/order/details?sn=' + order_sn
      })
    }
})

           